variacni.rada <- function(X, row.names){
  X  <- as.numeric(X)
  U  <- sort(unique(X))
  nu <- length(U)
  nj <- rep(0, nu)
  for(j in 1:nu){
    nj[j] <- sum(X == U[j])
  }
  
  n  <- sum(nj)
  pj <- nj / n
  Nj <- cumsum(nj)
  Fj <- cumsum(pj)
  
  variacni.rada <- data.frame(nj = nj, pj = pj, Nj = Nj, Fj = Fj)
  row.names(variacni.rada) <- row.names
  variacni.rada
}

rel.barplot <- function(N, col = 1:length(N), border = 'black', names = 1:length(N), main = '', xlab = '', 
                        ylab = 'relativni cetnost', xlim = c(0.2, 2), ylim = c(-0.03, 1.03), 
                        density = 60, cex = 1, mtext = '', a = 0, axes = axes){
  n       <- sum(N)
  l       <- length(N)
  
  barplot(matrix(N / n, l, 1), col = col, border = border, density = density,
          main = main, xlim = xlim, ylim = ylim, ylab = ylab, axes = T, las = 1)
  legend('topright',legend = rev(names), fill = rev(col), density = density, bty = 'n')
  mtext(xlab, 1, line = 1)
  
  stred <- 0.7
  cn    <- cumsum(N) / n
  cn2   <- (N / n) / 2
  vyska <- c(cn2[1], cn[1:(length(cn) - 1)] + cn2[2:length(cn2)])
  vyska <- vyska + a
  text(stred, vyska, paste(N, '; ',round(N / n * 100, 2),'%',sep = ''), cex = cex)
}


dotplot <- function(x, y, xlim = c(min(x), max(x)), ylim = c(min(y), max(y)), 
                    col = 'black', pch = 21, bg = 'white', lwd = 1, cex = 1,
                    xlab = 'x', ylab = 'y', main = '', las = 1){
  rand <- rnorm(length(x), 0, 0.03)
  x2 <- x + rand
  plot(x2, y, type = 'p', xlim = xlim, ylim = ylim, pch = pch, col = col, bg = bg, lwd = lwd,
       main = main, xlab = xlab, ylab = ylab, las = las)
}

norm2 <- function(x, y, mu1, mu2, sigma1, sigma2){
  rho <- 0
  Sigma <- matrix(c(sigma1 ^ 2, sigma2 * sigma1 * rho, sigma1 * sigma2 * rho, sigma2 ^ 2), 2, 2, byrow = T)
  xy <- c(x[1] - mu1, y[1] - mu2)
  konstanta <- 1 / (2 * pi * sqrt(sigma1 * sigma2 * (1 - rho ^ 2)))
  hustota <- konstanta * exp(- 1 / 2 * t(xy) %*% solve(Sigma) %*% xy)
  return(hustota)
}



rel.barplot.two <- function(n, col = 'grey40', col.number = 'white', density = NULL, border = 'black',
                            xlab = '', ylab = 'Relative Frequency', main = '', names = rep('', dim(t(t(n)))[2]), 
                            legend = 1:dim(t(t(n)))[1], cex.main = 1.2, cex = 1, las = 1){
  
  n <- t(t(n))
  s <- apply(n, 2, sum)
  r <- t(t(n) / s)
  d <- dim(n)[1]
  b <- dim(n)[2]
  
  n <- n[d:1, ]
  s <- s[b:1]
  r <- r[d:1, ]
  
  v <- (lower.tri(matrix(1, d, d)) + 1 / 2 * diag(d)) %*% r
  
  x <- rep(0.5:(b - 0.5), rep(d, b), main = '', xlab = xlab)
  
  barplot(t(t(r)), width = 1, space = 0, density = density, col = col, border = border,
          ylim = c(0, 1), xlim = c(0, b + 1), xlab = '', ylab = ylab, names = names, main = '', 
          cex.names = cex, axes = F )
  legend('topright', fill = rev(col), density = density, legend = legend, bty = 'n', cex = cex)
  text(x = x, y = v, paste(round(r, 4) * 100, '%'), col = col.number, cex = cex)
  axis(2, las = T)
  mtext(main, side = 3, font = 2, line = 1.7, cex = cex.main)
  mtext(xlab, side = 1, font = 1, line = 2.5, cex = cex)
}
